package com.hyc.myvideo.data.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hyc.myvideo.R;
import com.hyc.myvideo.data.adapter.base.BaseListAdapter;
import com.hyc.myvideo.data.api.model.ModelLive;
import com.hyc.myvideo.utils.imguitl.ImgUitl;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 直播列表适配器
 * @author gyb
 * @version 1.0
 * @created 2017/07/22
 */
public class LiveViewAdapter extends BaseListAdapter<ModelLive> {
    public LiveViewAdapter(Context context, List<ModelLive> objects) {
        super(context, objects);
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            if (getItemViewType(position) == 0) {
                holder = new ViewHolder();
                //无数据展示
                convertView = inflater.inflate(R.layout.adapter_itme_no, parent, false);
                holder.noDataRootLayout = (LinearLayout) convertView.findViewById(R.id.r_no_id);
            } else {
                convertView = inflater.inflate(R.layout.activity_adapter_live_item, parent, false);
                holder = new ViewHolder(convertView);
            }
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        if (hasNoData) {
            AbsListView.LayoutParams lp = new AbsListView.LayoutParams(getScreenWidth(), getScreenHeight() * 2 / 3);
            holder.noDataRootLayout.setLayoutParams(lp);
        } else {
            ModelLive modelLive = mDataList.get(position);
            setCharges(holder,modelLive);
        }
        return convertView;
    }

    public static class ViewHolder {
        public ViewHolder() {
        }
        public ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
        LinearLayout noDataRootLayout;
        //直播封面
        @BindView(R.id.live_img_cover)ImageView imgCover;
        //直播收费方式
        @BindView(R.id.live_img_type)ImageView imageViewType;
        //当前观看人数
        @BindView(R.id.live_t_number)TextView textViewNumber;
        //当前课程讲师
        @BindView(R.id.live_t_people)TextView textViewPeople;
        //简介
        @BindView(R.id.live_t_introduction) TextView textViewIntroduction;
    }

    /***
     * 展示收费方式
     * @param holder
     * @param modelLive
     */
    private void setCharges(ViewHolder holder,ModelLive modelLive){
        holder.imageViewType.setVisibility(View.VISIBLE);
        switch (modelLive.getType()){
            case 1:
                holder.imageViewType.setImageResource(R.mipmap.icon_logo_live_vip);
                break;
            case 2:
                holder.imageViewType.setImageResource(R.mipmap.icon_logo_live_pay);
                break;
            default:
                holder.imageViewType.setVisibility(View.GONE);
                break;
        }
        int width = getScreenWidth()/2 - 3*15;
        ImgUitl.setImg(modelLive.getImgUrl(), holder.imgCover,width ,ImgUitl.dip2px(mContext,110));
        String duration = String.format(mContext.getString(R.string.huc_my_live_number), "50");
        holder.textViewNumber.setText(modelLive.getNumber());
        holder.textViewPeople.setText(modelLive.getLecturer());
        holder.textViewIntroduction.setText(modelLive.getIntroduction());
    }
}
